package com.lk.xiteng.blog.service.impl;

import com.lk.xiteng.blog.entity.Blog;
import com.lk.xiteng.blog.repository.BlogRepository;
import com.lk.xiteng.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogRepository blogRepository;

    @Override
    public Blog insert(Blog blog) {
        blogRepository.save(blog);
        return null;
    }

    @Override
    public Blog findById(String id) {
        return blogRepository.findById(id).get();
    }

    @Override
    public Blog update(Blog blog) {
        blogRepository.save(blog);
        return null;
    }

    @Override
    public  Page<Blog> search(String content) {
       // QueryStringQueryBuilder queryBuilders=QueryBuilders.queryStringQuery(content).field("content");//左右模糊
        //blogRepository.findByTitleLikeOrContentLike("天气","天气",pageable).getContent().forEach((blog)-> System.out.println(blog.toString()));
        // return blogRepository.search(QueryBuilders.termsQuery("content",content),pageable);
        Pageable pageable = PageRequest.of(0,2);
        return blogRepository.search(QueryBuilders.queryStringQuery(content).field("content"),pageable);
    }

    @Override
    public void deleteAll() {
        blogRepository.deleteAll();
    }

   /* private SearchQuery getEntitySearchQuery(int start, int size, String searchContent) {
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
                .add(QueryBuilders.matchAllQuery(), //查询所有
                        ScoreFunctionBuilders.weightFactorFunction(100))

//                查询条件，但是并未使用，放在这里，为的是将来使用，方便参考，知道如何用
//                .add(QueryBuilders.matchPhraseQuery("name", searchContent),
//                      ScoreFunctionBuilders.weightFactorFunction(100))
                //设置权重分 求和模式
                .scoreMode("sum")
                //设置权重分最低分
                .setMinScore(10);

        // 设置分页
        Sort sort  = new Sort(Sort.Direction.DESC,"id");
        Pageable pageable = new PageRequest(start, size,sort);
        return new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(functionScoreQueryBuilder).build();
    }*/
}
